package com.ruyuan2020.im.code.l618.redis;


public class RedisKeyUtil {

    private static final String SPLIT = ":";
    private static final String PREFIX_ENTITY_LIKE = "like:entity";
    private static final String PREFIX_USER_LIKE = "like:user";

    // 某个实体的赞
    // like:entity:entityType:entityId -> set(userId)
    public static String getEntityLikeKey(int entityType, int entityId) {
        return PREFIX_ENTITY_LIKE + SPLIT + entityType + SPLIT + entityId;
    }

    // 某个用户的赞
    // like:user:userId -> int
    public static String getUserLikeKey(int userId) {
        return PREFIX_USER_LIKE + SPLIT + userId;
    }

    // 点赞
//    public void like(int userId, int entityType, int entityId, int entityUserId) {
//        redisTemplate.execute(new SessionCallback() {
//            @Override
//            public Object execute(RedisOperations operations) throws DataAccessException {
//                String entityLikeKey = RedisKeyUtil.getEntityLikeKey(entityType, entityId);
//                boolean isMember = operations.opsForSet().isMember(entityLikeKey, userId);
//                operations.multi();
//                if (isMember) {
//                    operations.opsForSet().remove(entityLikeKey, userId);
//                } else {
//                    operations.opsForSet().add(entityLikeKey, userId);
//                }
//                return operations.exec();
//            }
//        });
//    }
}